package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;


@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 获取今日运营数据
     * @return
     */
    public BusinessDataVO businessData() {

        LocalDate startDate = LocalDate.now();
        LocalDateTime bigDate = LocalDateTime.of(startDate, LocalTime.MIN);
        LocalDateTime endDate = LocalDateTime.of(startDate, LocalTime.MAX);

        Map map = new HashMap();
        map.put("begin", bigDate);
        map.put("end", endDate);

        // 新增用户数量
        Integer newUsers = userMapper.countByDate(map);

        // 总订单数量
        Integer totalOrderCount = orderMapper.getCountStatus(map);

        map.put("status", Orders.COMPLETED);
        // 有效订单数量(已完成)
        Integer validOrderCount = orderMapper.getCountStatus(map);
        BigDecimal voc = new BigDecimal(validOrderCount);

        // 营业额
        BigDecimal turnover = orderMapper.getTurnover(map);
        if (turnover == null){
            turnover = new BigDecimal(0);
        }

        // 平均客单价
        BigDecimal unitPrice = new BigDecimal(0);
        if (turnover.compareTo(BigDecimal.ZERO) != 0 && voc.compareTo(BigDecimal.ZERO) != 0 ){
            unitPrice = turnover.divide(voc);
        }

        // 订单完成率
        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0){
            orderCompletionRate = (double) validOrderCount / totalOrderCount;
        }

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover.doubleValue())
                .unitPrice(unitPrice.doubleValue())
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        Map map = new HashMap();
        map.put("begin", begin);

        // 全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.getByStatusAndTime(map);
        // 已取消数量
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.getByStatusAndTime(map);
        // 已完成数量
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.getByStatusAndTime(map);
        // 待派送数量
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.getByStatusAndTime(map);
        // 待接单数量
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.getByStatusAndTime(map);
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 获取菜品总览
     * @return
     */
    public DishOverViewVO overviewDishes() {
        Map map = new HashMap();
        // 已停售菜品数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.getCountByStatus(map);
        // 已启售菜品数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.getCountByStatus(map);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 获取套餐总览
     * @return
     */
    public DishOverViewVO overviewSetmeal() {
        Map map = new HashMap();
        // 已停售套餐数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.getCountByStatus(map);
        // 已启售套餐数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.getCountByStatus(map);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}